<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="gl_searchBox gl_mb_15">
      <el-row :gutter="20">
        <el-col :md="6" :lg="4">
          <p>置顶筛选</p>
          <el-select v-model="form.IsTop" size="mini" placeholder="请选择" clearable filterable @change="getData()">
            <el-option label="全部" value="" />
            <el-option label="未置顶" :value="false" />
            <el-option label="已置顶" :value="true" />
          </el-select>
        </el-col>
        <el-col :md="6" :lg="4">
          <p>商品名称</p>
          <el-input v-model="form.Filter" placeholder="请输入商品名称" size="mini" clearable @change="getData()" />
        </el-col>
        <el-col :md="6" :lg="4">
          <p>商家名称</p>
          <el-input v-model="form.StoreNameFilter" placeholder="请输入商家名称" size="mini" clearable @change="getData()" />
        </el-col>
        <el-col :md="8" :lg="6">
          <el-button style="margin-top:27px" type="primary" size="mini" @click="getData()">查询</el-button>
          <el-button style="margin-top:27px" type="warning" size="mini" @click="reset()">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" :data="goodsData" class="gl_table_noBorder" size="mini" @sort-change="changeSort">
      <el-table-column label="商品信息" align="center" width="70">
        <template slot-scope="{row}">
          <img class="roodsPhoto" :src="getStaticFileUrl(row.images[0].imageUrl)" alt="" @error="errorPhoto" @mouseenter="showGoodsImg(...arguments, row.images[0].imageUrl)" @mouseleave="hideGoodsImg(row.images[0].imageUrl)">
        </template>
      </el-table-column>
      <el-table-column label="" min-width="300">
        <template slot-scope="{row}">
          {{ row.name || '--' }}
        </template>
      </el-table-column>
      <el-table-column label="价格" sortable="custom" width="120">
        <template slot-scope="{row}">
          <span>
            {{ row.price || row.price === 0 ? '￥' + row.price/100 : '--' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="storeName" label="商家" min-width="180" />
      <el-table-column label="操作" width="200">
        <template slot-scope="{row}">
          <el-button v-if="!row.isTopped" size="mini" type="text" @click="goTop(row)">置顶商品</el-button>
          <el-button v-else size="mini" type="text" @click="cancleTop(row)">取消置顶</el-button>
        </template>
      </el-table-column>
    </el-table>
    <img v-if="isShowImg && getStaticFileUrl(bigImg)" class="roodsBigPhoto" :style="position" :src="getStaticFileUrl(bigImg)" alt="" @error="errorPhoto">
    <el-row class="gl_pt_20" style="text-align: center;">
      <el-pagination
        :current-page="form.Page"
        :page-size="form.MaxResultCount"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        :page-sizes="[10, 20, 50, 100]"
        @size-change="sizeChange"
        @current-change="pageIndexChange"
      />
    </el-row>
  </div>
</template>

<script>
import defaultImg from '@/assets/images/face_default.png';
import {
  api_GET_MallProduct
} from '@/extend/api/shoppingmallswaggerApi/app.js';
import {
  api_POST_MallProductTop,
  api_DELETE_MallProductTop
} from '@/extend/api/mall.js';
export default {
  data() {
    return {
      defaultImg: defaultImg,
      loading: false,
      totalPage: 0, // 分页总条目数
      form: {
        IncludeStoreName: true,
        IncludeImages: true,
        IncludeTop: true,
        IncludeStatus: true,
        ProductStatusCodes: ['OnShelf'],
        IsTop: '',
        Filter: '',
        StoreNameFilter: '',
        Sorting: '',
        Page: 1,
        SkipCount: 0,
        MaxResultCount: 10
      },
      goodsData: [], // 表格数据
      position: {
        left: 0,
        top: 0
      },
      isShowImg: false,
      bigImg: ''
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取表格数据
    getData(page) {
      this.loading = true;
      this.form.Page = page || 1;
      this.form.SkipCount = (this.form.Page - 1) * this.form.MaxResultCount;
      this.form.Sorting = this.form.Sorting || 'creationTime DESC';
      api_GET_MallProduct(this.form).then(res => {
        this.loading = false;
        this.goodsData = res.items;
        this.totalPage = res.totalCount;
      }).catch((error) => {
        this.loading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 重置搜索表单
    reset() {
      this.form = {
        IncludeStoreName: true,
        IncludeImages: true,
        IncludeTop: true,
        IncludeStatus: true,
        ProductStatusCodes: ['OnShelf'],
        IsTop: '',
        Filter: '',
        StoreNameFilter: '',
        Sorting: '',
        Page: 1,
        SkipCount: 0,
        MaxResultCount: 10
      };
      this.getData();
    },
    // 置顶商品
    goTop(row) {
      var that = this;
      this.$confirm(`您确定要置顶 ${row.name} 吗?`, '置顶商品', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = { productId: row.id };
        api_POST_MallProductTop(data).then(res => {
          that.$message({
            type: 'success',
            message: '置顶成功!'
          });
          that.getData(that.form.Page);
        }).catch((error) => {
          const _msg = error.error.message || error.message;
          that.$message({ type: 'error', message: _msg });
        });
      });
    },
    // 取消置顶
    cancleTop(row) {
      var that = this;
      this.$confirm(`您确定要取消 ${row.name} 的置顶吗?`, '取消置顶', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = { productId: row.id };
        api_DELETE_MallProductTop(data).then(res => {
          that.$message({
            type: 'success',
            message: '操作成功!'
          });
          that.getData(that.form.Page);
        }).catch((error) => {
          const _msg = error.error.message || error.message;
          that.$message({ type: 'error', message: _msg });
        });
      });
    },
    // 显示放大图片
    showGoodsImg(event, img) {
      const _this = this;
      _this.bigImg = img;
      const _img = event.target; // 这里就是获取到的dom元素
      const _position = _img.getBoundingClientRect();
      this.timeOut = setTimeout(() => {
        _this.isShowImg = true;
        _this.$set(_this.position, 'left', _position.left + 60 + 'px');
        _this.$set(_this.position, 'top', _position.top + 'px');
      }, 300);
    },
    hideGoodsImg() {
      const _this = this;
      clearTimeout(this.timeOut);
      _this.isShowImg = false;
    },
    // 价格排序
    changeSort (val) {
      console.log(val);
      // 升序："Price"，倒序："Price DESC"
      if (val.column.label === '价格' && val.order === 'ascending') {
        this.form.Sorting = 'Price';
      } else if (val.column.label === '价格' && val.order === 'descending') {
        this.form.Sorting = 'Price DESC';
      } else {
        this.form.Sorting = '';
      }
      this.getData(this.form.Page);
    },
    // 获取静态文件
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultImg;
    },
    // 分页
    sizeChange: function(MaxResultCount) {
      this.form.MaxResultCount = MaxResultCount;
      this.getData();
    },
    pageIndexChange(currentPage) {
      this.getData(currentPage);
    }
  }
};
</script>

<style lang="scss" scoped>
.preview_close,
.preview_top,
.preview_bottom {
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
  vertical-align: middle;
  color: rgba(0,0,0,0.65);;
}

.preview_close:hover,
.preview_top:hover,
.preview_bottom:hover {
  color: #5CBDFF;
}
.disable_move {
  color: #C0C4CC;
  cursor: not-allowed;
}
.roodsPhoto{
  width: 60px;
  height: 60px;
  vertical-align: middle;
}
.roodsBigPhoto{
  width: 310px;
  height: 280px;
  position: fixed;
  z-index: 10;
}
.icon_color {
  color: #1682e6;
}
</style>
